package com.mbc.common.manager;

import java.util.List;

import com.mbc.common.entities.BranchType;
import com.mbc.common.data.BranchTypeDataList;
import com.mbc.common.param.CriteriaParams;

public class BranchTypeMgr extends AbstractMgr {
	
	public BranchTypeMgr(){}
	
	public BranchTypeMgr(String sessionId){
		this.sessionId = sessionId;
	}
	
	public void create(BranchType entity) throws Exception {
		openConnection();
		try {
			beginTransaction();
			entityManager.persist(entity);
			commitTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void update(BranchType entity) throws Exception {
		openConnection();
		try {
			beginTransaction();
			entityManager.merge(entity);
			commitTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void delete(long key) throws Exception {
		openConnection();
		try {
			beginTransaction();
			BranchType l_BranchType = entityManager.find(BranchType.class, key);
			entityManager.remove(l_BranchType);
			commitTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public BranchType find(long key) throws Exception {
		openConnection();
		try {
			return entityManager.find(BranchType.class, key);
		} catch (Exception e) {
			
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public List<BranchType> findAll()throws Exception{
		openConnection();
		try {
			return entityManager.createNamedQuery("BranchType.findAll",  BranchType.class).getResultList();
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public BranchTypeDataList findBranchTypes(CriteriaParams cri)throws Exception{
		openConnection();
		try {
            String l_cri = " where bt.name like '%" + cri.getKeyword() + "%'";
            String l_searchJPQL = "select bt from Branch bt" + l_cri;
            
            List<BranchType> l_branchTypeList = entityManager.createQuery(l_searchJPQL, BranchType.class)
                    .setFirstResult(cri.getIndex())
                    .setMaxResults(cri.getSize())
                    .getResultList();
            
            BranchTypeDataList l_dataList = new BranchTypeDataList();
            l_dataList.setEntityList(l_branchTypeList);
            
            if(cri.getIndex() == 0){
            	String l_countQuery = "select count(bt.branchTypeId) from Branch bt" + l_cri;
            	l_dataList.setTotalRecord(entityManager.createQuery(l_countQuery, Long.class).getSingleResult());
            }
			return l_dataList;
		} catch (Exception e) {
			throw e;
		} finally{
			closeConnection();
		}
	}
	/*
	public JasperPrint prepareBranchTypeReport(BranchTypeRpt entity) throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			branchTypeDAO = CommonDAOFactory.getBranchTypeDAO(getConnection());

			JRBeanCollectionDataSource l_ds = new 
			JRResultSetDataSource l_jrRS = new  new JRResultSetDataSource(branchTypeDAO.prepareBranchTypeReport(entity));
			// String l_path = ServerGlobal.getAppPath();

			 JasperReport l_jsRpt =(JasperReport)JRLoader.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/BranchType.jasper");
			// l_params.put("Cri",info.getCri());
			 HashMap<String, Object> l_params = new HashMap<String, Object>();
				l_params.put("companyname", "mbc");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}*/
}
